package com.yugong.controller;

import com.yugong.common.Result;
import com.yugong.service.AccountService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author yugong
 * @date 2024/2/28
 */
@RestController
@RequestMapping("/files")
public class FileController {
    @Resource
    private AccountService accountService;
    private String accountNo;

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 文件路径
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        String avatarPath = accountService.uploadAvatar(file, getAccountNo());
        return Result.success(avatarPath);
    }

    /**
     * 文件下载
     *
     * @param fileName 文件名
     * @param response 响应
     * @return 响应码
     */
    @GetMapping("/download")
    public Result download(String fileName, HttpServletResponse response) {
        boolean downloadFlag = accountService.downloadAvatar(fileName, response);
        return Result.success(downloadFlag);
    }

    /**
     * 获取账号
     *
     * @param accountNo 账号
     * @return 账号
     */
    @GetMapping("/get-account-no/{accountNo}")
    public void getAccountNo(@PathVariable String accountNo) {
        setAccountNo(accountNo);
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }
}
